package com.runshu.newsmartark.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.runshu.newsmartark.R;
import com.runshu.newsmartark.base.BaseActivity;
import com.runshu.newsmartark.base.MyApplication;
import com.runshu.newsmartark.common.ConstantUrl;
import com.runshu.newsmartark.entity.GetQRCodeEntity;
import com.runshu.newsmartark.entity.UserInfoEntity;
import com.runshu.newsmartark.event.Event;
import com.runshu.newsmartark.event.EventBusUtils;
import com.runshu.newsmartark.event.EventCode;
import com.runshu.newsmartark.mvp.contract.GetQRCodeContract;
import com.runshu.newsmartark.mvp.contract.QRcodeAuthContract;
import com.runshu.newsmartark.mvp.model.GetQRCodeModel;
import com.runshu.newsmartark.mvp.model.QRcodeAuthModel;
import com.runshu.newsmartark.mvp.presenter.GetQRCodePresenter;
import com.runshu.newsmartark.mvp.presenter.QRcodeAuthPresenter;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.reader.base.ReaderBase;
import com.runshu.newsmartark.reader.helper.ReaderHelper;
import com.runshu.newsmartark.reader.helper.ReaderSetting;
import com.runshu.newsmartark.util.LodDialogClass;
import com.runshu.newsmartark.util.MacUtils;
import com.runshu.newsmartark.util.PollingUtil;
import com.runshu.newsmartark.util.SoundPoolUtils;
import com.runshu.newsmartark.util.common.LogUtil;
import com.runshu.newsmartark.util.common.ToastUtils;
import com.runshu.newsmartark.util.widget.CommonQRDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/8/26
 * @decs：
 */

public class LoginActivity extends BaseActivity implements QRcodeAuthContract.View, GetQRCodeContract.View {
    @BindView(R.id.img_back)
    ImageView mImgBack;
    @BindView(R.id.btn_reader_card)
    Button mBtnReaderCard;
    @BindView(R.id.btn_face)
    Button mBtnFace;
    @BindView(R.id.btn_qr_code)
    Button mBtnQrCode;
    private static final String TAG = "LoginActivity";
    private CommonQRDialog QRDia;
    private PollingUtil pollingUtil;
    private Runnable runnable;
    private int count = 1;
    private QRcodeAuthPresenter qRcodeAuthPresenter;
    private GetQRCodePresenter getQRCodePresenter;
    private String path;
    private ReaderHelper mReaderHelper;
    private static ReaderSetting m_curReaderSetting;
    Intent intent;
    private ReaderBase readerBase;

    @Override
    protected int getLayoutId() {
        return R.layout.act_login;
    }

    @Override
    protected void initView() {
        getQRCodePresenter = new GetQRCodePresenter(this, new GetQRCodeModel());
        qRcodeAuthPresenter = new QRcodeAuthPresenter(this, new QRcodeAuthModel());

    }

    @Override
    protected void initData() {
        try {
            mReaderHelper = ReaderHelper.getDefaultHelper();
            readerBase = mReaderHelper.getReader();
            m_curReaderSetting = mReaderHelper.getCurReaderSetting();
        } catch (Exception e) {
            e.printStackTrace();
        }

        getQRCodePresenter.getQRCode(MacUtils.getFormatMacAddr());
    }

    @Override
    protected void bindListener() {

    }

    @Override
    protected void afterRequestPermission(int requestCode, boolean isAllGranted) {

    }

    private void showQRDia(String path) {
        QRDia = new CommonQRDialog(context);
        QRDia.setCancelable(false);
        QRDia.setContent("扫码登录");
        QRDia.setContentHint("请用微信扫码登录");
        QRDia.setImgShow(R.mipmap.icon_qr_login);
        QRDia.setImgResour(path);
        QRDia.show();
        pollingUtil = new PollingUtil(new Handler(getMainLooper()));
        runnable = new Runnable() {
            @Override
            public void run() {
                LogUtil.e(TAG, "----------handler 定时轮询任务----------" + count);
                qRcodeAuthPresenter.getWxCodeLogin(MacUtils.getFormatMacAddr());
            }
        };
        pollingUtil.startPolling(runnable, 1500, true);
        QRDia.setOnClickListenerCancel(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                QRDia.dismiss();
                if (pollingUtil != null && runnable != null) {
                    pollingUtil.endPolling(runnable);
                }
            }
        });

    }

    @OnClick({R.id.img_back, R.id.btn_reader_card, R.id.btn_face, R.id.btn_qr_code})
    public void onViewClicked(View view) {
        intent = new Intent();
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_reader_card:
                intent.setClass(this, ReaderCardAuthActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_face:
                SoundPoolUtils.play(context, R.raw.raw_face, 0);
                intent.setClass(this, RegisterAndRecognizeActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_qr_code:
                SoundPoolUtils.play(context, R.raw.raw_shaoma, 0);
                showQRDia(path);

                break;
        }

    }


    @Override
    public void onGetWxCodeLoginSuccess(ResponseBean responseBean) {
        if (pollingUtil != null && runnable != null) {
            pollingUtil.endPolling(runnable);
        }
        UserInfoEntity userInfoEntity = (UserInfoEntity) responseBean.pullData();
        LodDialogClass.closeCustomCircleProgressDialog();
        MyApplication.application.setUserInfoEntity(userInfoEntity);
        loginSuccess();
        if (isAdmin()) {
            if (userInfoEntity.isAdmin()) {
                Intent intent = new Intent(context, ManagerActivity.class);
                startActivity(intent);
                finish();
            } else {
                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
                //发通知
                EventBusUtils.sendEvent(new Event(EventCode.EventNOTADMIN));
            }

        } else {
            connect();
        }

    }


    @Override
    public void onError(String msg) {
        ToastUtils.showText(msg);
    }

    @Override
    protected void onStop() {
        if (pollingUtil != null && runnable != null) {
            pollingUtil.endPolling(runnable);
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (pollingUtil != null && runnable != null) {
            pollingUtil.endPolling(runnable);
        }
        super.onDestroy();
    }

    @Override
    public void OnGetQRCodeSuccess(ResponseBean responseBean) {
        GetQRCodeEntity qrCodeEntity = (GetQRCodeEntity) responseBean.pullData();
        path = qrCodeEntity.getPath();
    }


    @Override
    public void OnError(String msg) {
        LodDialogClass.closeCustomCircleProgressDialog();
        ToastUtils.showText(msg);
    }

    private void connect() {
        //开灯
        readerBase.writeGpioValue(m_curReaderSetting.btReadId, (byte) 0x04, (byte) 0x01);
        //开门
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                readerBase.writeGpioValue(m_curReaderSetting.btReadId, (byte) 0x03, (byte) 0x01);
                Intent intent = new Intent(context, TakeStockDtaActivity.class);
                startActivityForResult(intent, 101);
                LodDialogClass.closeCustomCircleProgressDialog();
            }
        }, 100);
    }


}